package com.hc.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hc.constant.CategoryStatusConst;
import com.hc.domain.Category;
import com.hc.dto.CategoryDTO;
import com.hc.dto.ListCategoryDTO;
import com.hc.mapper.CategoryMapper;
import com.hc.service.CategoryService;
import com.hc.vo.CategorySortVO;
import com.hc.vo.CategoryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 博客类别
 *
 * @author 梁云亮
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public boolean getCategoryByName(String name) {
        boolean res = categoryMapper.selectCategoryAmountByName(name) == 1 ? true : false;
        return res;
    }

    @Override
    public CategoryDTO getCategoryById(Integer id) {
        CategoryDTO res = categoryMapper.selectedCategoryById(id);
        return res;
    }

    @Override
    public List<ListCategoryDTO> listUncheckedCategoriesByUserId(Integer userId) {
        List<ListCategoryDTO> res = categoryMapper.selectCategoryDTOsByUserId(userId, CategoryStatusConst.UNCHECKED);
        return res;
    }

    @Override
    public List<ListCategoryDTO> listCheckedCategoriesByUserId(Integer userId) {
        List<ListCategoryDTO> res = categoryMapper.selectCategoryDTOsByUserId(userId, CategoryStatusConst.CHECKED);
        return res;
    }

    @Override
    public List<ListCategoryDTO> listDeletedCategoriesByUserId(Integer userId) {
        List<ListCategoryDTO> res = categoryMapper.selectCategoryDTOsByUserId(userId, CategoryStatusConst.DELETED);
        return res;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int createCategory(CategoryVO categoryVO) {
        int res = categoryMapper.insertCategory(categoryVO);
        return res;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int modifyCategoryChecked(Integer id) {
        int res = categoryMapper.updateCategoryStatus(id, CategoryStatusConst.CHECKED);
        return res;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int modifyCategoryUnchecked(Integer id) {
        int res = categoryMapper.updateCategoryStatus(id, CategoryStatusConst.UNCHECKED);
        return res;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int modifyCategoryDeleted(Integer id) {
        int res = categoryMapper.updateCategoryStatus(id, CategoryStatusConst.DELETED);
        return res;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int modifyCategory(Integer id, CategoryVO categoryVO) {
        int res = categoryMapper.updateCategorySelective(id, categoryVO);
        return res;
    }

    @Override
    public int modifyCategoryLevel(CategorySortVO categorySortVO) {
        int res = categoryMapper.updateCategoryLevel(categorySortVO);
        return res;
    }

    @Override
    public int modifyCategoriesLevel(List<CategorySortVO> categorySortVOList) {
        int res = categoryMapper.updateCategoriesLevel(categorySortVOList);
        return res;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteCategoryById(Integer id) {
        int res = categoryMapper.deleteCategoryById(id);
        return res;
    }

}
